unit hadebasedataaccessor;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  hadebaseobject,
  hadebasevisitor,
  hadecriteria,
  hademapbase,
  hadeconnectionmanager,
  hadeobjectfactory,
  hadedataset,
  hademapping,
  sysutils;

Type
  { IHadeDataAccessor }

  IHadeDataAccessor = interface
    ['{8E05DEE1-9F5D-42DD-88F7-120AEF6AD2A7}']
    function getWithoutParams: boolean;
    procedure setWithoutParams(AValue: boolean);

    procedure Read(const ATableName: string; const AObject: THadeVisited;
      ACols, AOrderBy: TStringList; const whereClause: string = '';
      AOrderType: THadeOrderType = oASC; const limit: integer = 0;
      const offset: integer = 0);

    procedure Insert(const ATableName: string; const AObject: THadeVisited;
      ACols: THadeMapSimpleValue; const Execute: boolean = True);

    procedure Update(const ATableName: string; const AObject: THadeVisited;
      ACols: THadeMapSimpleValue; const whereClause: string;
      const Execute: boolean = True);

    procedure Delete(const ATableName, whereclause: string;
      const AObject: THadeVisited; const Execute: boolean = True);

    property withoutParams: boolean read getWithoutParams write setWithoutParams;

    function getDataSet: THadeDataSet;
    function getConnection: THadeConnection;
    procedure ExecuteMultiple(ASql: string);
  end;

  { THadeBaseDataAccessor }

  THadeBaseDataAccessor = class(THadeBaseObject, IHadeDataAccessor)
  protected
    fWithoutParams: boolean;
    fConnection: THadeConnection;
    FDataSet: THadeDataSet;
    FObjectFactory: THadeIOManager;
    FMappingManager:THadeMappingManager;
  public
    procedure Read(const ATableName: string; const AObject: THadeVisited;
      ACols, AOrderBy: TStringList; const whereClause: string = '';
      AOrderType: THadeOrderType = oASC; const limit: integer = 0;
      const offset: integer = 0); virtual;

    procedure Insert(const ATableName: string; const AObject: THadeVisited;
      ACols: THadeMapSimpleValue; const Execute: boolean = True); virtual;

    procedure Update(const ATableName: string; const AObject: THadeVisited;
      ACols: THadeMapSimpleValue; const whereClause: string;
      const Execute: boolean = True); virtual;

    procedure Delete(const ATableName, whereclause: string;
      const AObject: THadeVisited; const Execute: boolean = True); virtual;

    function getWithoutParams: boolean; virtual;
    procedure setWithoutParams(AValue: boolean); virtual;

    procedure ExecuteMultiple(ASql: string); virtual;

    function getDataSet: THadeDataSet; virtual;
    function getConnection: THadeConnection; virtual;

    constructor Create(AConnection: THadeConnection); virtual;
    destructor destroy;override;
  end;

implementation

{ THadeBaseDataAccessor }

procedure THadeBaseDataAccessor.Read(const ATableName: string;
  const AObject: THadeVisited; ACols, AOrderBy: TStringList;
  const whereClause: string; AOrderType: THadeOrderType; const limit: integer;
  const offset: integer);
begin
  //WriteLn('damn you call me!');
end;

procedure THadeBaseDataAccessor.Insert(const ATableName: string;
  const AObject: THadeVisited; ACols: THadeMapSimpleValue; const Execute: boolean = True);
begin

end;

procedure THadeBaseDataAccessor.Update(const ATableName: string;
  const AObject: THadeVisited; ACols: THadeMapSimpleValue;
  const whereClause: string; const Execute: boolean);
begin

end;

procedure THadeBaseDataAccessor.Delete(const ATableName, whereclause: string;
  const AObject: THadeVisited; const Execute: boolean);
begin

end;

function THadeBaseDataAccessor.getWithoutParams: boolean;
begin
  Result := fWithoutParams;
end;

procedure THadeBaseDataAccessor.setWithoutParams(AValue: boolean);
begin
  fWithoutParams := AValue;
end;

procedure THadeBaseDataAccessor.ExecuteMultiple(ASql: string);
begin
  fDataSet.executeMultiple(Asql);
end;

function THadeBaseDataAccessor.getDataSet: THadeDataSet;
begin
  Result := fDataset;
end;

function THadeBaseDataAccessor.getConnection: THadeConnection;
begin
  Result := fConnection;
end;

constructor THadeBaseDataAccessor.Create(AConnection: THadeConnection);
begin
  fConnection:=AConnection;
  FMappingManager:=GHadeMappingManager.Find(fConnection.ConnectionName);
  fDataSet:=THadeDataset.Create(fConnection);
  FObjectFactory := THadeIOManager.Create(FMappingManager);
end;

destructor THadeBaseDataAccessor.destroy;
begin
  FreeAndNil(FDataSet);
  FreeAndNil(FObjectFactory);
  inherited Destroy;
end;

end.

